package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.collect.Lists;
import com.yungu.swift.api.base.gateway.CurrentUser;
import com.yungu.swift.api.config.NacosListener;
import com.yungu.swift.assets.passenger.model.dto.PassengerAddressDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerDto;
import com.yungu.swift.assets.passenger.model.param.ApiPassengerAddressParam;
import com.yungu.swift.assets.passenger.model.vo.PassengerVo;
import com.yungu.swift.assets.passenger.service.PassengerAddressService;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.base.model.api.ApiAdCodeParam;
import com.yungu.swift.base.model.api.ApiParam;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.order.service.OrderEvaluateService;
import com.yungu.swift.system.marketing.model.dto.MarketingGiftDto;
import com.yungu.swift.system.marketing.model.vo.MarketingShareVo;
import com.yungu.swift.system.marketing.service.MarketingCouponService;
import com.yungu.swift.system.marketing.service.MarketingGiftService;
import com.yungu.swift.system.marketing.service.MarketingShareService;
import com.yungu.swift.system.sys.model.dto.SysAccountPassengerDto;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;

/**
 * Description: PasInfoApi
 * Author: cuixiuyin
 * Date:2019/11/01 16:35
 * Version:2.0
 */
@RestController
@RequestMapping(value = "/api/v2/passenger/token")
@Api(value = "【乘客】【用户信息】乘客信息接口V2", tags = "V2-乘客信息接口")
public class PasInfoApi {

    @Reference
    private PassengerService passengerService;
    @Reference
    private PassengerAddressService passengerAddressService;
    @Reference
    private OrderEvaluateService orderEvaluateService;
    @Reference
    private MarketingGiftService marketingGiftService;
    @Reference
    private MarketingShareService marketingShareService;
    @Reference
    private MarketingCouponService marketingCouponService;
    @Reference
    private SysAccountPassengerService SysAccountPassengerService;

    @ResponseImgOverride
    @ApiOperation(value = "乘客信息接口")
    @RequestMapping(value = {"/user/info"}, method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData<PassengerVo> info(@CurrentUser ApiParam apiParam) {
        return passengerService.selInfo(apiParam.getUserUuid());
    }

    @ApiOperation(value = "乘客修改手机号")
    @PostMapping(value = {"/user/mobile/update"})
    public ResponseData<Boolean> mobileUpdate(@RequestBody Map<String, String> paramMap, @CurrentUser ApiParam apiParam) {
        String mobile = paramMap.get("mobile");
        if (StringUtils.isEmpty(mobile)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        PassengerDto dto = passengerService.get(MapUtils.build("mobile", mobile)).getData();
        if (dto != null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "该手机号已失效");
        }
        // 1. 将原手机号置为失效
        return ResponseData.buildSuccessResponse(true);
    }

    @ResponseImgOverride
    @ApiOperation(value = "乘客信息设置接口")
    @PostMapping(value = {"/user/setInfo"})
    public ResponseData<PassengerVo> setInfo(@RequestBody Map<String, Object> paramMap, @CurrentUser ApiParam apiParam) {
        // 头像
        Object face = paramMap.get("face");
        // 性别 (1男，2女)
        Object sex = paramMap.get("sex");
        // 昵称
        Object nickName = paramMap.get("nickName");
        if (nickName != null) {
            SysAccountPassengerDto sysAccountPassengerDto = new SysAccountPassengerDto();
            sysAccountPassengerDto.setUuid(apiParam.getUserUuid());
            sysAccountPassengerDto.setAccountName(nickName.toString());
            sysAccountPassengerDto.setUpdateOn(new Date());
            ResponseData<Boolean> responseData = SysAccountPassengerService.edit(sysAccountPassengerDto);
            if (!responseData.isSuccess() || responseData.getData() == null || Objects.equals(responseData.getData(), Boolean.FALSE)) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "修改乘客账户名称失败！");
            }
        }
        PassengerDto passengerDto = new PassengerDto();
        if (nickName != null) {
            passengerDto.setNickname(nickName.toString());
        }
        if (sex != null) {
            passengerDto.setSex((Integer) sex);
        }
        if (face != null) {
            passengerDto.setFace(face.toString());
        }
        passengerDto.setUuid(apiParam.getUserUuid());
        passengerDto.setUpdateOn(new Date());
        passengerDto.setUpdateBy(apiParam.getUserUuid());
        passengerService.edit(passengerDto);
        return passengerService.selInfo(apiParam.getUserUuid());
    }

    @ApiOperation(value = "乘客地址信息接口")
    @RequestMapping(value = {"/address/list"}, method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData<List<PassengerAddressDto>> addressList(@CurrentUser ApiParam apiParam) {
        return passengerAddressService.addressList(apiParam.getUserUuid());
    }

    @ApiOperation(value = "乘客设置常用地址接口")
    @PostMapping(value = {"/address/add"})
    public ResponseData<PassengerAddressDto> addAddress(@RequestBody ApiPassengerAddressParam addressParam) {
        return passengerAddressService.addAddress(addressParam);
    }

    @ApiOperation(value = "乘客获取家/公司地址接口")
    @PostMapping(value = {"/address/info"})
    public ResponseData addressInfo(@RequestBody Map<String, Integer> paramMap, @CurrentUser ApiParam apiParam) {
        // 0.家地址, 1.公司地址 2.上下车地址
        Integer type = paramMap.get("type");
        if (type == null) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return passengerAddressService.getAddressInfo(apiParam.getUserUuid(), type);
    }

    @PostMapping(value = {"/marketing/isOpenCity"})
    @ApiOperation(value = "判断乘客位置是否开通乘客邀请好友城市", notes = "获取乘客邀请好友开通城市")
    public ResponseData<Map<String, Boolean>> isOpenCity(@RequestBody ApiAdCodeParam adCodeParam) {
        if (adCodeParam.getAdCode() == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "未获取定位点");
        }
        return marketingGiftService.isOpenCity(adCodeParam.getAdCode(), 2);
    }

    @RequestMapping(value = {"/share/info"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取乘客分享信息", notes = "获取乘客分享信息", response = ResponseData.class)
    public ResponseData listActivity(
            @io.swagger.annotations.ApiParam(name = "token", value = "token", required = true) @RequestParam String token,
            @io.swagger.annotations.ApiParam(name = "appid", value = "APP标识", required = true) @RequestHeader String appid) {
        Map<String, Object> map = MapUtils.build();
        map.put("token", token);

        PassengerDto passengerDto = passengerService.get(map).getData();
        if (passengerDto == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "找不到对应的登录用户");
        }
        String mobile = passengerDto.getMobile();
        map.clear();
        map.put("clientSide", 1);
        map.put("status", 1);
        MarketingShareVo marketingShareVo = marketingShareService.findVo(map).getData();
        //拼接url把邀请人的手机号和邀请人类型拼接到url中
        if (marketingShareVo != null) {
            StringBuilder linkUrlSb = new StringBuilder(NacosListener.SYS_CONFIG.getApiHost());
            linkUrlSb.append(marketingShareVo.getLinkUrl());
            if (marketingShareVo.getLinkUrl().indexOf(MagicConstant.SYMBOL_QUESTION_MARK) != -1) {
                linkUrlSb.append("&");
            } else {
                linkUrlSb.append("?");
            }
            linkUrlSb.append("appid=").append(appid).append("&inviter=").append(mobile).append("&inviterType=1");
            marketingShareVo.setLinkUrl(linkUrlSb.toString());
            marketingShareVo.setLinkTitle(NacosListener.SYS_CONFIG.getName());
            //获取乘客邀请礼包优惠券总额
            BigDecimal passengerMoney = marketingCouponService.getSumMoneyByTypes(Lists.newArrayList(2)).getData();
            if (passengerMoney == null) {
                passengerMoney = new BigDecimal(0);
            }
            marketingShareVo.setInviterMoney(passengerMoney);
            marketingShareVo.setInviteeMoney(passengerMoney);
            map.clear();
            map.put("appid", appid);
            map.put("type", 2);
            map.put("status", 1);
            MarketingGiftDto marketingGiftDto = (marketingGiftService.findGiftByMap(map)).getData();
            if (marketingGiftDto == null) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到数据", null);
            }
            marketingShareVo.setInvitedGiftName(marketingGiftDto.getInvitedGiftName());
            marketingShareVo.setInvitedRewardMethod(marketingGiftDto.getInvitedRewardMethod());
            marketingShareVo.setGetMode(marketingGiftDto.getGetMode());
            return ResponseData.buildSuccessResponse("获取成功", marketingShareVo);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到数据", null);
    }
}
